//
//  CZPhotoSelectorCell.swift
//  66.照片选择器
//
//  Created by apple on 16/3/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 1.定义协议
protocol CZPhotoSelectorCellDelegate: NSObjectProtocol {
    // 2.定义方法
    func photoSelectorCell(cell: CZPhotoSelectorCell, addButtonClick: UIButton)
    
    func photoSelectorCell(cell: CZPhotoSelectorCell, deleteButtonClick: UIButton)
}

/// 照片选择器cell
class CZPhotoSelectorCell: UICollectionViewCell {
    
    // MARK: - 属性
    // 要显示的图片
    var image: UIImage? {
        didSet {
            // 将图片设置给加号按钮
            addButton.setImage(image, forState: UIControlState.Normal)
            
            // 显示删除按钮
            deleteButton.hidden = false
        }
    }
    
    // 3.定义代理属性
    weak var delegate: CZPhotoSelectorCellDelegate?
    
    // 使用闭包
    // 1.定义闭包属性 String?
    // 加好按钮点击的闭包
    var addButtonCallBack: ((cell: CZPhotoSelectorCell) -> ())?
    
    // 删除按钮点击的闭包
    var deleteButtonCallBack: ((cell: CZPhotoSelectorCell) -> ())?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加控件
        contentView.addSubview(addButton)
        contentView.addSubview(deleteButton)
        
        // 2.添加约束
        addButton.translatesAutoresizingMaskIntoConstraints = false
        deleteButton.translatesAutoresizingMaskIntoConstraints = false
        
        let views = ["ab" : addButton, "db" : deleteButton]
        // VFL
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[ab]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[ab]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[db]-4-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-4-[db]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
    }
    
    // MARK: - 设置加号按钮
    func setupAddButton() {
        // 设置加号按钮图片
        addButton.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        
        // 隐藏删除按钮
        deleteButton.hidden = true
    }
    
    // MARK: - 按钮点击事件
    @objc private func addButtonClick(button: UIButton) {
        // 4.调用代理
        delegate?.photoSelectorCell(self, addButtonClick: button)
        
        // 2.调用闭包
        addButtonCallBack?(cell: self)
    }
    
    @objc private func deleteButtonClick(button: UIButton) {
        // 4.调用代理
        delegate?.photoSelectorCell(self, deleteButtonClick: button)
        
        // 2.调用闭包
        deleteButtonCallBack?(cell: self)
    }
    
    // MARK: - 懒加载
    /// 加号按钮
    private lazy var addButton: UIButton = {
        let button = UIButton()
        
        // 不要使用背景,因为背景设置图片后不能设置内容填充模式
        button.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        button.imageView?.contentMode = UIViewContentMode.ScaleAspectFill
        
        button.addTarget(self, action: "addButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        // 不要使用背景,因为背景设置图片后不能设置内容填充模式
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        button.addTarget(self, action: "deleteButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
